/*******************************************************************************
Replication Materials for Blau, Kahn, Brummund, Cook, and Larson-Koester "Is 
There Still Son Preference in the United States?"

Figure 4

Date Modified: 10/19/2019

*******************************************************************************/



*-------------------------------------------------------------------------------
                                   *LOAD DATA
*-------------------------------------------------------------------------------
use "$acs\acs_data", clear 


*-------------------------------------------------------------------------------
                                 *RESTRICT SAMPLE
*-------------------------------------------------------------------------------
keep if nonrkids == 0 & nonusbirth == 0 & sfrelate == 0 & father_sample != 1 ///
	& multi_sample != 1 & mult == 0 & female == 1 & oldc <=12 & ///
	inrange(age,18,40)==1 & foster_hh == 0 & nchild18>=1
drop if citizen==1 | marst == 5

*-------------------------------------------------------------------------------
                       *SET UP EXCEL FILE THAT WILL STORE ESTIMATES
*-------------------------------------------------------------------------------
putexcel set "$foutput\figure4_data", replace
putexcel A1 = "group" // native, immi or both
putexcel B1 = "educ" // education level, lhd, hs, sc, c
putexcel C1 = "coeff"
putexcel D1 = "sd"

forvalues a = 2/5 {

	local j = `a'
	local k = `a' + 4
	local l = `a' + 8

	putexcel A`j' = "native"
	putexcel A`k' = "immi"
	putexcel A`l' = "both"

}

putexcel B2 = "lhs"
putexcel B3 = "hs"
putexcel B4 = "sc"
putexcel B5 = "c"
putexcel B6 = "lhs"
putexcel B7 = "hs"
putexcel B8 = "sc"
putexcel B9 = "c"
putexcel B10 = "lhs"
putexcel B11 = "hs"
putexcel B12 = "sc"
putexcel B13 = "c"

*-------------------------------------------------------------------------------
                                 *REGRESSION LOOP
*-------------------------------------------------------------------------------
local controls "`controls' i.genrace i.region i.year age age2 age3"
local wgt "hhwtnorm"

local jkl = 2
foreach nat in 1 0 2 {
	foreach pup in "lths" "hs" "scol" "cold" {
		
	   preserve
	   
	   keep if `pup'==1

			if `nat'==0{
				local regnat "if native==`nat'"
			}
			else if `nat'==1{
				local regnat "if native==`nat'"
			}
			else {
				local regnat ""
			}
												
			reg femhdalt i.chld1 `controls' `regnat' [aw=`wgt'],  robust
			return list
			local coef = r(table)[1, 2]
			local se =  r(table)[2, 2]
			putexcel C`jkl' = `coef'
			putexcel D`jkl' = `se'
			local jkl = `jkl' + 1
				
	   restore
	   
		}			
		
	}
	
	
*-------------------------------------------------------------------------------
                               *PREPARE FIGURE DATA
*-------------------------------------------------------------------------------
import excel "$foutput\figure4_data", clear firstrow 
gen ll95 = coeff - 1.96*sd 
gen ul95 = coeff + 1.96*sd 

// native
matrix median = J(4, 3, .)
matrix coln median = median ll95 ul95
matrix rown median = 1 2 3 4 
local i = 1
foreach e in lhs hs sc c {
	sum coeff if group == "native" & educ == "`e'"
	local c = r(mean)
	sum ll95 if group == "native" & educ == "`e'"
	local l = r(mean)
	sum ul95 if group == "native" & educ == "`e'"
	local u = r(mean)
	matrix median[`i',1] = `c', `l', `u'
	local i = `i' + 1
}
matrix list median

// immi
matrix median2 = J(4, 3, .)
matrix coln median2 = median ll95 ul95
matrix rown median2 = 1 2 3 4 
local i = 1
foreach e in lhs hs sc c {
	sum coeff if group == "immi" & educ == "`e'"
	local c = r(mean)
	sum ll95 if group == "immi" & educ == "`e'"
	local l = r(mean)
	sum ul95 if group == "immi" & educ == "`e'"
	local u = r(mean)
	matrix median2[`i',1] = `c', `l', `u'
	local i = `i' + 1
}
matrix list median2

*-------------------------------------------------------------------------------
                                   *FIGURE CODE
*-------------------------------------------------------------------------------
coefplot (matrix(median[,1]), ci((median[,2] median[,3]))  mcolor(black) ciopts(color(black))) ///
(matrix(median2[,1]), ci((median2[,2] median2[,3])) msymbol(D) mcolor(gs5)  ciopts(color(gs5))), ///
graphregion(color(white)) bgcolor(white) ///
xtitle(First Child Girl, size(vsmall)) ///
xline(0, lpattern(shortdash) lcolor(black)) ///
xlabel(,labsize(vsmall)) ///
ylabel(,labsize(vsmall)) ///	
legend(position(4) ring(0) col(1) lab(2 "Native") lab(4 "Immigrant") size(vsmall)) ///
coeflabels(1 = "Less than high school" 2 = "High School" 3 = "Some College" 4 = "College +") 
graph export "$foutput\figure4.png", replace

